package kr.hkit.powerjava.chapter22.work2.Dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import kr.hkit.powerjava.chapter22.work2.Vo.Student;

public class StudentMenegerImp implements ItemDaoInterface<Student> {

	private ArrayList<Student> arList;

	public StudentMenegerImp(ArrayList<Student> arList) {
		this.arList = arList;
	}

	@Override
	public void addItem(Student Item) {
		arList.add(Item);
	}

	@Override
	public void delItem(Student Item) {
		arList.remove(arList.indexOf(Item));
	}

	@Override
	public void updateItem(Student Item) {
		if (searchItem(Item)) {
			arList.set(arList.indexOf(Item), Item);
		} else {
			System.out.println("수정 실패");
		}
	}

	@Override
	public boolean searchItem(Student Item) {
		return arList.contains(Item);
	}

	public void prnItems(Comparator<Student> comp) {
		prnHeader();
		Collections.sort(arList, comp);
		for (Student s : arList) {
			System.out.println(s);
		}
	}

	private void prnHeader() {
		String str = String.format("%s\t %s\t %s\t %s\t %s\t %s\t %s", "학번",
				"성명", "국어", "영어", "수학", "총점", "평균");
		System.out.println(str);
		System.out.println("-----------------------------------");
	}

	@Override
	public void prnItem() {
		prnHeader();
		for (Student s : arList) {
			System.out.println(s);
		}
	}

}
